<!--
 * @Author: qiaoxin
 * @Date: 2024-04-09 19:35:26
 * @LastEditors: qiaoxin
 * @LastEditTime: 2024-04-16 12:03:50
 * @Description: 
 * @FilePath: \erp-app\src\pages\pm\requirement\detail.vue
-->

<route lang="json5">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '需求单评审',
    enablePullDownRefresh: true,
    navigationStyle: 'custom',
  },
}
</route>

<template>
  <page-meta :root-font-size="setting.rootFontSize"></page-meta>
  <view style="min-height: 100vh; background-color: #f2f2f1">
    <view style="position: sticky; top: 0; z-index: 9999">
      <wd-navbar title="需求单评审" :bordered="false" safeAreaInsetTop>
        <template #left>
          <wd-icon name="thin-arrow-left" size="22px" color="white" @click="goBack"></wd-icon>
        </template>
      </wd-navbar>
    </view>

    <view style="flex: 1; overflow: auto">
      <th-card title="基础信息">
        <th-form>
          <th-form-item label="单据编号">
            <text class="text">{{ form.demandCode }}</text>
          </th-form-item>
          <th-form-item label="日期">
            <text class="text">{{ formatByDayjs(form.demandDate) }}</text>
          </th-form-item>
          <th-form-item label="客户" v-if="form.cusFlag == '0'">
            <text class="text">{{ form.cusCode }}</text>
          </th-form-item>
          <th-form-item :label="form.cusFlag == '0' ? '业务员' : '需求人'">
            <text class="text">{{ form.personName }}</text>
          </th-form-item>
          <th-form-item label="业务部门" v-if="form.cusFlag == '0'">
            <text class="text">{{ form.deptName }}</text>
          </th-form-item>
          <!-- <th-form-item label="所属公司" v-if="form.cusFlag == '0'">
            <text class="text seq-no-text">{{ form.tenantName }}</text>
          </th-form-item> -->
        </th-form>
      </th-card>
      <th-card title="产品需求信息">
        <th-form>
          <th-form-item label="需求类型">
            <text class="text">{{ form.demandType }}</text>
          </th-form-item>
          <th-form-item label="需求产品">
            <text class="text">{{ form.invName }}</text>
          </th-form-item>
          <!-- <th-form-item label="指标" v-if="isShowCheckTarget" class="form-CheckTarget">
            <wd-table :data="checkTargetList">
              <wd-table-col prop="cchkguidename" label="检验指标名称" width="33%"></wd-table-col>
              <wd-table-col prop="checktarget" label="U8指标" width="33%"></wd-table-col>
              <wd-table-col prop="cusCheckTarget" label="客户指标" width="34%"></wd-table-col>
            </wd-table>
          </th-form-item> -->

          <th-form-item label="需求产品规格">
            <text class="text">{{ form.invStd }}</text>
          </th-form-item>
          <th-form-item label="产品英文名称" v-if="form.invEnName">
            <text class="text">{{ form.invEnName }}</text>
          </th-form-item>
          <th-form-item label="用途">
            <text class="text">{{ form.applicationType }}</text>
          </th-form-item>
          <th-form-item label="年需求量/吨">
            <text class="text">{{ form.invDemand }}</text>
          </th-form-item>
          <th-form-item label="是否有外来样">
            <text class="text">{{ form.sampleFlagName }}</text>
          </th-form-item>
          <template v-if="showMoreDemand">
            <th-form-item label="外来样样品编码" v-if="form.sampleFlag != '0'">
              <text class="text">{{ form.sampleCode }}</text>
            </th-form-item>
            <th-form-item label="外来样样品数量" v-if="form.sampleFlag != '0'">
              <text class="text">{{ form.samplesNum }}{{ form.sampleUnit }}</text>
            </th-form-item>
            <th-form-item label="外来样批次" v-if="form.sampleFlag != '0'">
              <text class="text">{{ form.sampleBatch }}</text>
            </th-form-item>
            <th-form-item label="要求完成时间">
              <text class="text">{{ formatByDayjs(form.askCompleteTime) }}</text>
            </th-form-item>

            <th-form-item label="是否给客户提供样品">
              <text class="text">{{ form.requestFlag == '1' ? '是' : '否' }}</text>
            </th-form-item>
            <th-form-item label="给客户提供样品数量" v-if="form.requestFlag == '1'">
              <text class="text">{{ form.requestNum }}{{ form.requestNumUnit }}</text>
            </th-form-item>
          </template>
          <th-form-item
            label="附件"
            class="form-demandDesc form-file"
            v-if="form.rdDemandFileEntity && form.rdDemandFileEntity.length"
          >
            <view class="form-file-view">
              <view
                v-for="(file, index) in form.rdDemandFileEntity"
                :key="file.fileUrl"
                class="info-row-item"
              >
                <text @click="previewFileHandle(file.fileUrl)">{{ file.fileName }}</text>
              </view>
            </view>
          </th-form-item>
          <th-form-item label="需求描述" class="form-demandDesc">
            <text class="text" @click="dbclickdemandDesc">{{ form.demandDesc }}</text>
          </th-form-item>
          <th-form-item label="下一步任务" class="form-demandDesc" v-if="form.wayName">
            <text class="text">{{ form.wayName }}</text>
          </th-form-item>
          <th-form-item label="评审结论" class="form-demandDesc" v-if="form.reviewConclusion">
            <text class="text">{{ form.reviewConclusion }}</text>
          </th-form-item>
          <th-form-item label="评审结论描述" class="form-demandDesc" v-if="form.demandResult">
            <text class="text">{{ form.demandResult }}</text>
          </th-form-item>
          <th-form-item
            label="预计完成日期"
            class="form-demandDesc"
            v-if="form.estimatedCompletionTime"
          >
            <text class="text">
              {{ form.estimatedCompletionTime ? formatByDayjs(form.estimatedCompletionTime) : '' }}
            </text>
          </th-form-item>
          <th-form-item label="回复" class="form-demandDesc" v-if="form.reply">
            <text class="text">{{ form.reply }}</text>
          </th-form-item>
          <th-form-item label="需求反馈方式" class="form-demandDesc" v-if="form.replyNeed != 3">
            <text class="text">
              <!-- {{ form.replyNeed == 1 ? '回复' : form.replyNeed == 0 ? '不回复' : '' }} -->
              {{ getReplyNeed(form.replyNeed) }}
            </text>
          </th-form-item>
          <th-form-item
            label="需求回复附件"
            class="form-demandDesc"
            v-if="form.rdReviewFileEntity && form.rdReviewFileEntity.length"
          >
            <view class="info-list">
              <view
                class="info-list-main"
                style="padding-top: 0; padding-left: 0; margin-top: 0; border: none"
              >
                <view class="info-list-row">
                  <!-- <view class="info-list-label">附件</view> -->
                  <view class="info-list-value">
                    <view style="display: flex; flex-direction: column">
                      <view
                        v-for="file in form.rdReviewFileEntity"
                        :key="file.id"
                        @click="previewFileHandle(file.fileUrl)"
                        class="info-row-item"
                      >
                        {{ file.fileName }}
                      </view>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </th-form-item>
          <!-- <th-form-item label="评审结果">
            <text class="text">
              <view v-if="form.rdFlag == '1'" class="result-tag">通过</view>
              <view
                v-else-if="form.rdFlag == '0'"
                class="result-tag"
                style="background-color: #f50"
              >
                不通过
              </view>
            </text>
          </th-form-item> -->
          <th-form-item label="是否立项" class="form-demandDesc" v-if="form.petitionFlag !== null">
            <text class="text">
              {{ form.petitionFlag == 1 ? '立项' : form.petitionFlag == 0 ? '不立项' : '' }}
            </text>
          </th-form-item>
          <th-form-item label="项目名称" class="form-demandDesc" v-if="form.petitionFlag == 1">
            {{ form.petitionName }}
          </th-form-item>
          <th-form-item
            label="是否推荐现有产品"
            class="form-demandDesc"
            v-if="form.wayCode == '30' && form.recommendFlag !== null"
          >
            <text class="text">
              {{ form.recommendFlag == 1 ? '是' : form.recommendFlag == 0 ? '否' : '' }}
            </text>
          </th-form-item>
          <th-form-item label="推荐产品名称" class="form-demandDesc" v-if="form.recommendFlag == 1">
            <text class="text">
              {{ form.recommendInvName }}
            </text>
          </th-form-item>
          <th-form-item label="推荐产品规格" class="form-demandDesc" v-if="form.recommendFlag == 1">
            <text class="text">
              {{ form.recommendInvStd }}
            </text>
          </th-form-item>

          <!-- <th-form-item label="预计完成时间">
          <text class="text">{{ formatByDayjs(form.completeTime) }}</text>
        </th-form-item> -->

          <!-- <th-form-item label="技术负责人">
            <text class="text">{{ form.technologyHeadName }}</text>
          </th-form-item> -->
          <!-- <th-form-item
            v-for="item in rdProjectUserList.filter((item) => item.chooseFlag == 1)"
            :label="item.documentsPostName"
            :key="item.id"
          >
            <text class="text">{{ getProjectName(item) }}</text>
          </th-form-item> -->
          <!-- <th-form-item label="项目成员" class="form-demandDesc">
            <text class="text">{{ getProjectName({ documentsPostCode: 0 }) }}</text>
          </th-form-item> -->

          <!-- <th-form-item label="备注">
          <text class="text">{{ form.remark }}</text>
        </th-form-item> -->
          <view class="exportModel" @click="showMoreDemandhandle">
            <text>{{ showMoreDemand ? '收起' : '展开' }}</text>
          </view>
        </th-form>
      </th-card>

      <th-card title="需求指标" v-if="isShowCheckTarget">
        <view style="overflow: auto" class="table-container">
          <table class="analysis-table">
            <tbody>
              <tr>
                <th style="width: 320rpx; min-width: 9.5rem">检验指标名称</th>
                <th style="width: 280rpx; min-width: 280rpx">U8指标</th>
                <th style="width: 280rpx; min-width: 280rpx">客户指标</th>
                <th style="width: 280rpx; min-width: 280rpx">描述</th>
              </tr>
              <tr
                align="center"
                v-for="(item, index) in checkTargetList"
                :key="index"
                :class="[item.checktarget != item.cusCheckTarget ? 'line-red' : '']"
              >
                <td>
                  <view>
                    {{ item.cchkguidename }}
                  </view>
                </td>
                <td>
                  <view>
                    {{ item.checktarget }}
                  </view>
                </td>
                <td>
                  <view>
                    {{ item.cusCheckTarget }}
                  </view>
                </td>
                <td>
                  <view>
                    {{ item.standardDesc }}
                  </view>
                </td>
              </tr>
            </tbody>
          </table>
        </view>
      </th-card>

      <th-card title="资料收集" v-if="isShowNewProduct">
        <view class="info-list">
          <view v-for="item in form.rdInformationDTO" :key="item.id" class="info-list-main">
            <view class="info-list-row">
              <view class="info-list-label">资料名称</view>
              <view class="info-list-value">{{ item.informationName }}</view>
            </view>
            <view class="info-list-row">
              <view class="info-list-label">资料描述</view>
              <view class="info-list-value">{{ item.informationDesc }}</view>
            </view>
            <view
              class="info-list-row"
              v-if="item.rdInformationProfileEntity && item.rdInformationProfileEntity.length"
            >
              <view class="info-list-label">附件</view>
              <view class="info-list-value">
                <view style="display: flex; flex-direction: column">
                  <view
                    v-for="file in item.rdInformationProfileEntity"
                    :key="file.id"
                    @click="previewFileHandle(file.profileUrl)"
                    class="info-row-item"
                  >
                    {{ file.profileInfo }}
                  </view>
                </view>
              </view>
            </view>
            <view class="info-list-row" v-if="item.informationWeb">
              <view class="info-list-label">网址</view>
              <view class="info-list-value info-row-item" @click="previewWeb(item.informationWeb)">
                {{ item.informationWeb }}
              </view>
            </view>
          </view>
        </view>
      </th-card>

      <th-card title="分析手段" v-if="isShowAnalysis">
        <text class="text">
          <view style="display: flex; flex-wrap: wrap">
            <view
              v-for="item in form.rdSampleAnalysisEntity"
              :key="item.id"
              class="result-tag"
              style="margin-right: 10rpx; background-color: #4d80f0"
            >
              {{ item.analysisName }}
            </view>
          </view>
        </text>
        <view style="overflow: auto" class="table-container">
          <table class="analysis-table">
            <tbody>
              <tr>
                <th style="width: 320rpx; min-width: 9.5rem">名称</th>
                <th style="width: 280rpx; min-width: 280rpx">说明</th>
              </tr>
              <tr align="center" v-for="(item, index) in form.rdSampleAnalysisEntity" :key="index">
                <td>
                  <view>
                    {{ item.analysisName }}
                  </view>
                </td>
                <td>
                  <view>
                    {{ item.analysisDesc }}
                  </view>
                </td>
              </tr>
            </tbody>
          </table>
        </view>
      </th-card>

      <th-card title="合成路线" v-if="isShowCarf">
        <view class="info-list">
          <view v-for="item in form.rdCraftEntity" :key="item.id" class="info-list-main">
            <view class="info-list-row">
              <view class="info-list-label">名称</view>
              <view class="info-list-value">{{ item.craftName }}</view>
            </view>

            <view class="info-list-row">
              <view class="info-list-label">反应条件</view>
              <view class="info-list-value">{{ item.craftDesc }}</view>
            </view>

            <view class="info-list-row">
              <view class="info-list-label">路线</view>
              <view class="info-list-value">
                <!-- <image
                    class="craft-row-img"
                    :src="item.craftUrl"
                    mode="scaleToFill"
                    @click="previewFileHandle(item.craftUrl)"
                  /> -->
                <wd-img
                  :width="140"
                  :height="140"
                  :src="item.craftUrl"
                  mode="aspectFit"
                  :enable-preview="true"
                />
              </view>
            </view>

            <view class="info-list-row">
              <view class="info-list-label">是否选择</view>
              <view class="info-list-value">
                <wd-switch
                  disabled
                  v-model="item.isUse"
                  :active-value="1"
                  :inactive-value="0"
                  style="margin: 5rpx 0"
                  size="small"
                ></wd-switch>
              </view>
            </view>
          </view>
        </view>
      </th-card>

      <th-card title="调查问卷" v-if="isQuestion">
        <!-- <view class="info-list">
          <view class="info-list-row">
            <view class="info-list-label">附件</view>
            <view class="info-list-value">
              <view style="display: flex; flex-direction: column">
                <view
                  v-for="file in form.rdTaskFileEntity"
                  :key="file.id"
                  @click="previewFileHandle(file.fileUrl)"
                  class="info-row-item"
                >
                  {{ file.fileName }}
                </view>
              </view>
            </view>
          </view>
        </view> -->

        <view class="info-list">
          <view class="info-list-main">
            <view class="info-list-row">
              <view class="info-list-label">附件</view>
              <view class="info-list-value">
                <view style="display: flex; flex-direction: column">
                  <view
                    v-for="file in form.rdTaskFileEntity"
                    :key="file.id"
                    @click="previewFileHandle(file.fileUrl)"
                    class="info-row-item"
                  >
                    {{ file.fileName }}
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </th-card>

      <!-- <th-card title="评审建议" v-if="demandOpinionUserName == '2006031401' || form.demandOpinion">
        <view style="width: 100%">
          <view
            v-if="
              !(
                form.reviewStatus != 2 &&
                form.taskAllStatus == 1 &&
                demandOpinionUserName == '2006031401'
              )
            "
            class="form-demandDesc"
          >
            <text class="text">
              {{ form.demandOpinion }}
            </text>
          </view>
          <view v-else>
            <wd-textarea
              v-model="form.demandOpinion"
              placeholder="请填写评审建议"
              auto-height
              :maxlength="2000"
              show-word-limit
              no-border
              prop="demandOpinion"
              :disabled="
                !(
                  form.reviewStatus != 2 &&
                  form.taskAllStatus == 1 &&
                  demandOpinionUserName == '2006031401'
                )
              "
            />
          </view>
        </view>
        <wd-button
          :round="false"
          style="width: 100%; margin-top: 20rpx"
          @click="saveDemandOpinion"
          v-if="
            form.reviewStatus != 2 &&
            form.taskAllStatus == 1 &&
            demandOpinionUserName == '2006031401'
          "
        >
          保存
        </wd-button>
      </th-card> -->

      <th-card
        title="评审意见"
        v-if="
          (opinionList && opinionList.length) || (form.reviewStatus != 2 && form.taskAllStatus == 1)
        "
      >
        <view style="width: 100%">
          <view
            style="overflow: auto"
            class="table-container"
            v-if="opinionList && opinionList.length"
          >
            <table class="analysis-table">
              <tbody>
                <tr>
                  <th style="width: 100rpx; min-width: 100rpx">姓名</th>
                  <th style="width: 280rpx; min-width: 280rpx">评审意见</th>
                </tr>
                <tr align="center" v-for="(item, index) in opinionList" :key="index">
                  <td>
                    <view>
                      {{ item.createName }}
                    </view>
                  </td>
                  <td>
                    <view>
                      {{ item.reviewOpinion }}
                    </view>
                  </td>
                </tr>
              </tbody>
            </table>
          </view>
          <view v-if="form.reviewStatus != 2 && form.taskAllStatus == 1">
            <wd-textarea
              v-model="opinionParasm.reviewOpinion"
              placeholder="请填写评审意见"
              auto-height
              :maxlength="2000"
              show-word-limit
              no-border
              prop="reviewOpinion"
              style="min-height: 200rpx"
              :disabled="!(form.reviewStatus != 2 && form.taskAllStatus == 1)"
            />
          </view>
        </view>
        <wd-button
          :round="false"
          style="width: 100%; margin-top: 20rpx"
          @click="saveDemandOpinion"
          v-if="form.reviewStatus != 2 && form.taskAllStatus == 1"
        >
          保存
        </wd-button>
      </th-card>

      <th-card title="评审人员签字">
        <th-form>
          <!-- <th-form-item label="副总工">
            <wd-img
              :width="200"
              :height="100"
              :src="form.deputyChiefEngineerUrl"
              @click="preview(form.deputyChiefEngineerUrl)"
            />
          </th-form-item>
          <th-form-item label="总工">
            <wd-img
              :width="200"
              :height="100"
              :src="form.chiefEngineerUrl"
              @click="preview(form.chiefEngineerUrl)"
            />
          </th-form-item> -->
          <!-- rdProjectUserList -->

          <th-form-item
            v-for="item in form.rdProjectUserEntity.filter((item) => item.signatureFlag == 1)"
            :label="item.documentsPostName"
          >
            <!--  @click="preview(getProjectName(item, 1))" -->
            <wd-img :width="200" :height="100" :src="getProjectName(item, 1)" />
          </th-form-item>

          <th-form-item
            v-for="item in form.rdProjectUserEntity.filter(
              (item) => item.documentsPostCode == '255' && item.userUrl,
            )"
            :label="item.documentsPostName"
          >
            <!--  @click="preview(getProjectName(item, 1))" -->
            <wd-img :width="200" :height="100" :src="getProjectName(item, 1)" />
          </th-form-item>
        </th-form>
      </th-card>

      <!-- <view :class="['sign-main', 'bg-white', showSign ? '' : 'sign-gray']" @click="sign">
        签字
      </view> -->
      <view :class="['sign-main', 'bg-white']" @click="sign" v-if="showSignProject">签字</view>

      <wd-action-sheet v-model="showAction" title="" @close="closeSign" @click-modal="closeSign">
        <view class="sign-main-show">
          <!-- <text class="sign-main-show-title">签字</text> -->
          <view class="sign-main-show-line">
            <!-- <wd-img :width="200" :height="100" :src="signature" @click="preview(signature)" /> -->
            <wd-img :width="200" :height="100" :src="signature" @click="signThis" />
          </view>
          <!-- <view class="sign-main-show-btn" @click="signThis">使用该签字</view> -->
          <view class="sign-main-show-btn" @click="signReset">重新签字</view>
        </view>
      </wd-action-sheet>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { useSetting } from '@/stores/setting';
import {
  getObj,
  getPersonByPersonCode,
  updateByIdEvery,
  uploadSignature,
  selectrdDocumentsPostUser,
  updaterdProjectUser,
  selectrdDocumentsPostDict,
  updateSignHandle,
  updateByIdOpinion,
  saveOpinion,
  updateOpinion,
  getOpinion,
} from '@/service/pm-requirement';
import { formatByDayjs } from '@/utils/util';
import { useUserStore } from '@/stores/user';
import signV from '../../index/sign.vue';

const setting = useSetting();
const userStore = useUserStore();
const showSign = ref(false);
const signature = ref('');
const userInfo = ref({});
const isChiefEngineer = ref('');
const isDeputyChiefEngineer = ref('');
const showAction = ref(false);
const isShowCheckTarget = ref(false);
const checkTargetList = ref([]);
const postUserObj: any = ref({});
const rdProjectUserList = ref([]); // 签名列表
const isShowNewProduct = ref(false);
const isShowAnalysis = ref(false);
const isShowCarf = ref(false);
const isQuestion = ref(false);
const showSignProject = ref(false);
const demandOpinionUserName = ref('');

// 提交表单数据
const form = reactive({
  id: '',
  demandDate: '',
  personName: '',
  demandType: null,
  deptHead: '',
  deptCode: '',
  applicationType: '',
  samplesNum: '',
  demandDesc: '',
  sampleCoding: '',
  askCompleteTime: null,
  taskCode: '',
  userCode: '',
  invName: '',
  invDemand: '',
  sampleFlag: null,
  demandCode: '',
  cusName: '',
  personCode: '',
  deptHeadCode: '',
  cusFlag: '0',
  demandResult: '',
  completeTime: '',
  // technologyHead: '',
  // technologyHeadName: '',
  remark: '',
  // technologyHeadCode: '',
  taskId: null,
  instanceId: '',
  invStd: '',
  sampleCode: '',
  sampleBatch: '',
  deptName: '',
  rdFlag: null,
  chiefEngineer: '',
  deputyChiefEngineer: '',
  status: '',
  sampleFlagName: '',
  deputyChiefEngineerUrl: '',
  chiefEngineerUrl: '',
  sampleUnit: '',
  cusCode: null,
  rdProjectUserEntity: [],
  wayName: '',
  rdInformationDTO: [],
  rdCraftEntity: [],
  rdSampleAnalysisEntity: [],
  rdTaskFileEntity: [],
  requestFlag: '0',
  requestNum: '',
  requestNumUnit: '',
  rdDemandFileEntity: [],
  reply: '',
  tenantName: '',
  demandOpinion: null,
  petitionFlag: null,
  petitionName: null,
  recommendFlag: null,
  recommendInvName: null,
  recommendInvStd: null,
  reviewConclusion: '',
  estimatedCompletionTime: [],
  rdReviewFileEntity: null,
  replyNeed: null,
  invEnName: null,
});

const init = (id) => {
  uni.$off('pm-requirement-sign');
  selectrdDocumentsPostDictList();
  uni.showLoading({
    mask: true,
  });
  isShowCheckTarget.value = false;
  getObj({ id })
    .then((res) => {
      console.log('res', res);
      Object.assign(form, res.data);

      if (res.data.rdDemandStandard && res.data.rdDemandStandard.length) {
        isShowCheckTarget.value = true;
        // 判断有多少行
        // const line = [];
        // res.data.rdDemandStandard.forEach((item) => {
        //   const o = line.find((l) => l == item.standardName);
        //   if (!o) {
        //     line.push(item.standardName);
        //   }
        // });
        // const allList = [];
        // line.forEach((item) => {
        //   const l = res.data.rdDemandStandard.filter((e) => e.standardName == item);
        //   let o = {};
        //   l.forEach((item) => {
        //     if (item.addFlag == 0) {
        //       if (item.standardType == 1) {
        //         o['cchkguidename'] = item.standardName;
        //         o['cchkguidecode'] = item.standardCode;
        //         o['checktarget'] = item.standardValue;
        //       } else {
        //         o['cchkguidename'] = item.standardName;
        //         o['cchkguidecode'] = item.standardCode;
        //         o['cusCheckTarget'] = item.standardValue;
        //       }
        //     } else {
        //       o['cchkguidename'] = item.standardName;
        //       o['cchkguidecode'] = item.standardCode;
        //       o['cusCheckTarget'] = item.standardValue;
        //     }
        //     o['addFlag'] = item.addFlag;
        //     o['id'] = item.id;
        //   });
        //   allList.push(o);
        // });

        // checkTargetList.value = allList;

        checkTargetList.value = res.data.rdDemandStandard.map((item) => {
          return {
            ...item,
            cchkguidename: item.standardName,
            cchkguidecode: item.standardCode,
            checktarget: item.standardValue,
            cusCheckTarget: item.standardValuePm,
          };
        });

        checkTargetList.value = checkTargetList.value.filter((item) => item.delFlag != 1);
      } else {
        isShowCheckTarget.value = false;
        checkTargetList.value = [];
      }
      // if (res.data.rdProjectUserEntity && res.data.rdProjectUserEntity.length) {
      //   rdProjectUserList.value = res.data.rdProjectUserEntity.map((item) => {});
      // }
      isShowNewProduct.value = false;
      isShowCarf.value = false;
      isQuestion.value = false;
      isShowAnalysis.value = false;

      try {
        const reviewWayCodeList = res.data.wayCode ? res.data.wayCode.split(',') : [];
        if (
          reviewWayCodeList.includes('31') ||
          reviewWayCodeList.includes('32') ||
          reviewWayCodeList.includes('33')
        ) {
          reviewWayCodeList.value.push('3');
        }
        if (
          reviewWayCodeList.includes('1') &&
          res.data.rdInformationDTO &&
          res.data.rdInformationDTO.length
        ) {
          // isShowNewProduct.value = true;
        } else {
          isShowNewProduct.value = false;
        }

        if (
          (reviewWayCodeList.includes('2') || reviewWayCodeList.includes('12')) &&
          res.data.rdSampleAnalysisEntity &&
          res.data.rdSampleAnalysisEntity.length
        ) {
          isShowAnalysis.value = true;

          // 获取最新版本的
          const { version } = res.data;
          if (version.length > 0) {
            const v = version[0].version;
            form.rdSampleAnalysisEntity = res.data.rdSampleAnalysisEntity.filter(
              (item) => item.version == v,
            );
          }
        } else {
          isShowAnalysis.value = false;
        }

        if (
          reviewWayCodeList.includes('3') &&
          res.data.rdCraftEntity &&
          res.data.rdCraftEntity.length
        ) {
          // isShowCarf.value = true;
        } else {
          isShowCarf.value = false;
        }

        if (
          reviewWayCodeList.includes('4') &&
          res.data.rdTaskFileEntity &&
          res.data.rdTaskFileEntity.length
        ) {
          isQuestion.value = true;
        } else {
          isQuestion.value = false;
        }

        // 判断能不能签字
        const { userInfos } = userStore;
        const cusPerson = userInfos.user.username;
        demandOpinionUserName.value = cusPerson;

        const o = form.rdProjectUserEntity.find((item) => item.userCode == cusPerson);

        console.log('-----o----', o);

        if (o) {
          showSignProject.value = true;
        } else {
          showSignProject.value = false;
        }
      } catch (error) {
        console.log('err', error);
      }
      rdReviewOpinionHandle();
    })
    .finally(() => {
      uni.hideLoading();
      uni.stopPullDownRefresh();
      selectrdDocumentsPostUserFn();
    });
  // checkPost();
};

onLoad((options) => {
  console.log('opp', options);
  const { id } = options;
  init(id);
});

const checkPost = () => {
  // 获取用户的岗位 和签字
  // getPersonByPersonCode
  const { userInfos } = userStore;
  const cusPerson = userInfos.user.username;
  getPersonByPersonCode({
    personCode: cusPerson,
  }).then((res) => {
    const sign = res.data.signature;
    signature.value = sign;
    const { hrPersonDeptList } = res.data;
    const postNameList = hrPersonDeptList.map((item) => item.postName); // 岗位列表
    // 判断是否有总工 、 副总工
    const _isChiefEngineer = postNameList.find(
      (item) => item && item.indexOf('副总工') < 0 && item.indexOf('总工') >= 0,
    );
    const _isDeputyChiefEngineer = postNameList.find((item) => item && item.indexOf('副总工') >= 0);
    isChiefEngineer.value = _isChiefEngineer;
    isDeputyChiefEngineer.value = _isDeputyChiefEngineer;
    if (_isChiefEngineer || _isDeputyChiefEngineer) {
      showSign.value = true;
    } else {
      showSign.value = false;
    }
    userInfo.value = res.data;
  });
};

const sign = () => {
  // if (!showSign.value) return;

  // 判断能不能签字
  if (form.rdFlag == null) {
    uni.showToast({
      title: '没有评审结果，不允许签字',
      icon: 'none',
    });
    return;
  }

  // 判断签字
  const { userInfos } = userStore;
  const cusPerson = userInfos.user.username;
  getPersonByPersonCode({
    personCode: cusPerson,
  }).then((res) => {
    const sign = res.data.signature;
    signature.value = sign;
    userInfo.value = res.data;
    if (sign) {
      showAction.value = true;
    } else {
      uni.$on('pm-requirement-sign', (e) => {
        console.log('sign', e);
        signHanle(e);
      });
      uni.navigateTo({
        url: '/pages/pm/requirement/autograph',
      });
    }
  });

  // uni.navigateTo({
  //   url: '/pages/pm/requirement/autograph',
  // });
};

onPullDownRefresh(() => {
  init(form.id);
});

const goBack = () => {
  uni.navigateBack();
};

// uni.$on('pm-requirement-sign', (e) => {
//   console.log('sign', e);
//   signHanle(e);
// });

const preview = (url) => {
  if (!url) return;
  uni.previewImage({
    urls: [url],
  });
};

const closeSign = () => {
  showAction.value = false;
  uni.$off('pm-requirement-sign');
};

let signT = new Date().getTime();
const signHanle = (sign) => {
  const nSignT = new Date().getTime();
  if (nSignT - signT < 1000) {
    return;
  }
  signT = new Date().getTime();
  const userInfos: any = userInfo.value;
  // // 判断岗位
  // const _isChiefEngineer = isChiefEngineer.value;
  // const _isDeputyChiefEngineer = isDeputyChiefEngineer.value;

  // const params = {
  //   id: form.id,
  // };

  // if (!_isChiefEngineer && !_isDeputyChiefEngineer) {
  //   uni.showToast({
  //     title: '只有总工和副总工才能签字',
  //     icon: 'none',
  //   });
  //   return;
  // }

  // if (_isChiefEngineer) {
  //   // params['chiefEngineer'] = userInfos.id;
  //   params['chiefEngineer'] = userInfos.personCode;
  //   params['chiefEngineerName'] = userInfos.personName;
  //   params['chiefEngineerPost'] = _isChiefEngineer;
  //   params['chiefEngineerUrl'] = sign;
  // } else if (_isDeputyChiefEngineer) {
  //   params['deputyChiefEngineer'] = userInfos.personCode;
  //   params['deputyChiefEngineerName'] = userInfos.personName;
  //   params['deputyChiefEngineerPost'] = _isDeputyChiefEngineer;
  //   params['deputyChiefEngineerUrl'] = sign;
  // }
  // const userInfos: any = userStore.userInfos;
  console.log('postUserObj', postUserObj.value);
  const params = {
    demandCode: form.demandCode,
    personId: userInfos.id,
    userName: userInfos.personName,
    userCode: userInfos.personCode,
    userUrl: sign,
    // userType: postUserObj.value.documentsPostCode,
    documentsPostName: postUserObj.value.documentsPostName,
    documentsPostCode: postUserObj.value.documentsPostCode,
    documentsName: postUserObj.value.documentsName,
    documentsCode: postUserObj.value.documentsCode,
    signatureFlag: postUserObj.value.signatureFlag,
    reviewFlag: postUserObj.value.reviewFlag,
    rdCode: form.demandCode,
  };

  uni.showLoading({
    mask: true,
  });
  updateSignHandle(params)
    .then((res) => {
      if (res.code == 0) {
        uni.showToast({
          title: '签字成功',
        });

        // setTimeout(() => {
        //   uni.navigateBack();
        // }, 500);

        // 更新签字
        if (!signature.value) {
          uploadSignature({
            id: userInfos.id,
            signature: sign,
          });
        }
        showAction.value = false;
        // 刷新页面
        init(form.id);
      } else {
        uni.showToast({
          mask: true,
          title: `签字失败,原因${res.msg}`,
          icon: 'none',
        });
      }
    })
    .finally(() => {
      uni.hideLoading();
      // uni.$off('pm-requirement-sign');
      // saveDemandOpinion();
    });
};

const signThis = () => {
  const sign = signature.value;
  signHanle(sign);
};

const signReset = () => {
  uni.$on('pm-requirement-sign', (e) => {
    console.log('sign', e);
    signHanle(e);
  });
  uni.navigateTo({
    url: '/pages/pm/requirement/autograph',
  });
};

const selectrdDocumentsPostDictList = async () => {
  try {
    const res = await selectrdDocumentsPostUser({
      webSql: `documents_code = '1'`,
      size: 1000,
    });
    if (res.code !== 0) {
      return;
    }
    rdProjectUserList.value = res.data.records;
  } catch (err) {
    console.log('err', err);
  }
};

const selectrdDocumentsPostUserFn = () => {
  const { userInfos } = userStore;
  const cusPerson = userInfos.user.username;

  selectrdDocumentsPostUser({
    webSql: `user_code = '${cusPerson}' and documents_code = 1`,
    size: 1000,
  })
    .then((res) => {
      console.log('res.data.records', res);
      if (res.code == 0) {
        if (res.data.records.length) {
          const lrs = res.data.records.find(
            (item) => item.reviewFlag == 1 && item.signatureFlag == 1,
          );
          const lr = res.data.records.find((item) => item.reviewFlag == 1);
          const l = res.data.records;
          let o: any = {};

          if (lrs.length) {
            o = lrs[0];
          } else if (lr.length) {
            o = lr[0];
          } else {
            o = l[0];
          }
          postUserObj.value = o;
          if (o.reviewFlag == 1 || o.signatureFlag == 1) {
            showSign.value = true;
          } else {
            showSign.value = false;
          }
        } else {
          showSign.value = false;

          // 查projectuser
          const projectuser = form.rdProjectUserEntity;
          const o = form.rdProjectUserEntity.filter((f) => f.userCode == cusPerson);
          postUserObj.value = o || {};
        }
      } else {
        showSign.value = false;
      }
    })
    .catch((err) => {
      showSign.value = false;
      console.log('res.data.records', err);
    });
};

// type 0 获取姓名 1 获取签名
const getProjectName = (item, type = 0) => {
  console.log(form.rdProjectUserEntity, item);
  const l = form.rdProjectUserEntity.filter(
    (f) => f.userCode == item.userCode && f.userType == item.documentsPostCode,
  );
  let s = '';
  if (l.length) {
    console.log('s', s);
    if (type == 0) {
      s = l.map((item) => item.userName).join(',');
      return s;
    }
    // s = l.map((item) => item.userUrl).join(',');
    s = l[0].userUrl;
    return s;
  }
  return s;
};

const previewFileHandle = (file) => {
  uni.navigateTo({
    url: `/pages/pm/preview-online?url=${file}`,
  });
};

const previewWeb = (url) => {
  uni.navigateTo({
    url: `/pages/pm/preview-online?url=${url}&isWeb=1`,
  });
};

let clickTime = 0;
const dbclickdemandDesc = () => {
  const nowT = new Date().getTime();
  if (nowT - clickTime <= 500) {
    clickTime = null;

    uni.navigateTo({
      url: `/pages/pm/requirement/look-demand-desc?id=${form.id}`,
    });
  } else {
    clickTime = new Date().getTime();
  }
};

const opinionParasm = reactive({
  reviewOpinion: '',
  reviewType: 0,
  id: null,
});
const opinionList = ref([]);

const saveDemandOpinion = () => {
  const params = { ...opinionParasm, demandId: form.id, demandCode: form.demandCode };
  uni.showLoading({
    mask: true,
  });
  (opinionParasm.id ? updateOpinion(params) : saveOpinion(params))
    .then((res) => {
      if (res.code != 0) {
        uni.showToast({
          title: res.msg,
          icon: 'none',
        });
        return;
      }
      init(form.id);
    })
    .finally(() => {
      uni.hideLoading();
    });
};

const showMoreDemand = ref(false);
const showMoreDemandhandle = () => {
  showMoreDemand.value = !showMoreDemand.value;
};

const rdReviewOpinionHandle = () => {
  uni.showLoading({
    mask: true,
  });
  getOpinion({
    demandCode: form.demandCode,
  })
    .then((res) => {
      if (res.code != 0) {
        uni.showToast({
          mask: true,
          title: `获取数据失败，失败原因：${res.msg}`,
          icon: 'none',
        });
        return;
      }
      opinionList.value = res.data;
      const { userInfos } = userStore;
      const cusPerson = userInfos.user.username;
      const o = res.data.find((item) => item.createBy == cusPerson);
      opinionParasm.reviewOpinion = o.reviewOpinion;
      opinionParasm.id = o.id;
    })
    .finally(() => {
      uni.hideLoading();
    });
};

const getReplyNeed = (replyNeed) => {
  if (replyNeed == 3) return '';
  const replyList = [
    {
      label: '不反馈',
      value: 0,
    },
    {
      label: '直接反馈',
      value: 1,
    },
    {
      label: '总经理确认后反馈',
      value: 2,
    },
  ];

  const o = replyList.find((item) => item.value == replyNeed);
  return o ? o.label : '';
};
</script>

<style lang="scss" scoped>
@import '../experiment/components/table';

.card-detail {
  padding: 0 !important;
}

.wd-cell {
  padding-left: 0 !important;
}

.wd-card__title {
  border-left: 4px solid #2e5cf6 !important;
}

.pm-table {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  padding: 0 30rpx;

  .pm-title {
    box-sizing: border-box;
    padding-left: 20rpx;
    margin: 40rpx 0;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    border-left: 8rpx solid #2e5cf6;
    border-radius: 4rpx;
  }

  .pm-row {
    display: flex;
    min-height: 60rpx;
    border-bottom: 1px solid #f6f0f0;

    .pm-label {
      display: flex;
      align-items: center;
      width: 20%;
      font-size: 26rpx;
      color: #979494;
    }

    .pm-value {
      display: flex;
      flex: 1;
      align-items: center;
      font-size: 26rpx;
      color: #333;
    }
  }
}

.sign-main {
  // display: flex;
  // align-items: center;
  // justify-content: center;
  // width: 100%;
  // margin: 20rpx 0;
  position: fixed;
  right: 0rpx;
  bottom: 40rpx;
  width: 160rpx;
  height: 80rpx;
  line-height: 80rpx;
  color: white;
  text-align: center;
  background-color: rgb(77 128 240);

  // border-radius: 50%;
  border-top-left-radius: 40rpx;
  border-bottom-left-radius: 40rpx;
}

:deep(.label) {
  width: 160rpx !important;
}

.result-tag {
  padding: 6rpx 14rpx;
  margin: 4rpx;
  font-size: 24rpx;
  color: white;
  background-color: #87d068;
  border-radius: 4rpx;
}

.sign-main-show {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .sign-main-show-title {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 80rpx;
    margin-bottom: 20rpx;
    font-size: 30rpx;
    font-weight: bold;
    color: #818181;
    border-bottom: 1px solid #e3e3e3;
  }

  .sign-main-show-line {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 90%;
    padding: 20rpx;
    margin: 20rpx 0;
    border: 4rpx solid #d0dbff;
    border-radius: 10rpx;
  }

  .sign-main-show-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 80rpx;
    border-top: 1px solid #e3e3e3;
  }
}

.sign-gray {
  background-color: gray;
}

.form-demandDesc {
  align-items: flex-start !important;

  .text {
    word-break: break-all;
  }
}

.form-CheckTarget {
  flex-direction: column !important;
  align-items: start !important;
}

.info-list {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.info-list-main {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 10rpx;
  margin: 10rpx 0;
  border: 1px solid #e3e3e3;
  border-radius: 8rpx;

  .info-list-row {
    display: flex;
    width: 100%;

    .info-list-label {
      width: 150rpx;
      font-size: 28rpx;
      color: #808080;
    }

    .info-list-value {
      display: flex;
      flex: 1;
      align-items: center;
      width: 0;
      word-break: break-all;
      white-space: pre-line;
    }
  }
}

.craft-row {
  display: flex;
  align-items: center;

  .craft-row-img-main {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    width: 0;
  }
}

.craft-row-img {
  width: 240rpx;
  height: 240rpx;
}

:deep(.form-file) {
  .content {
    flex: 1 !important;
    width: 0 !important;
    word-break: break-all !important;
  }
}

.line-red td {
  background-color: #ffecec !important;
}

.exportModel {
  width: 100%;
  margin-top: 10rpx;
  font-size: 28rpx;
  color: #409eff;
  text-align: center;
}

.info-row-item {
  display: flex;
  justify-content: space-between;
  width: 100%;
  padding: 6rpx 0;
  color: blue;
  word-break: break-all;
}

:deep(.wd-textarea__inner) {
  min-height: 200rpx;
}
</style>
